import React, { Component } from 'react';
import { Button, Card, Row, Col, Layout, Menu, Pagination } from 'antd';
import './App.css';
import Cards from './Cards';


const { Footer, Sider, Content } = Layout;

class App extends Component {
  state = {
    total: 1,
    current: 1,
    items: [],
  }

  componentDidMount() {
    this.fetchPage(this.state.current);
  }

  handleClick(selectedID) {
    this.setState({
      items: this.state.items.filter(item => (item.id == selectedID))
    })
  }

  fetchPage(id) {
    fetch("http://localhost:8080/movies?startFrom=" + id + "&pageSize=12")
      .then(res => res.json())
      .then(result =>
        this.setState({
          isLoaded: true,
          items: result.content,
          total: result.totalElements
        })
      )
      .catch(error =>
        this.setState({
          isLoaded: true,
          error
        })
      );

  }



  render() {
    return (

      <Layout>

        <Layout>
          <Content >
            <div className="App">
              <Cards items={this.state.items} onClick={(i) => this.handleClick(i)} />
            </div>
          </Content>
        </Layout>
        <Footer>
          <Pagination showQuickJumper defaultCurrent={1} total={this.state.total} pageSize={12} current={this.state.current} onChange={(current) => this.fetchPage(current)} />
        </Footer>
      </Layout>

    );
  }
}



class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h2 style={{ Color: "#ECECEC" }}>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}





export default App;